package com.hhkj.captcha.common.checker;

import com.hhkj.captcha.common.manager.CaptchaManager;
import com.hhkj.captcha.model.extend.Captcha;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * CaptchaChecker
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/12/26 16:20
 */
@Component
public class CaptchaChecker {

    @Autowired
    private CaptchaManager captchaManager;

    /**
     * 检验手机验证码
     *
     * @param code        验证码
     * @param mobile      手机号
     * @param captchaCode 验证码
     */
    public void check(String code, String mobile, String captchaCode) {
        Captcha captcha
                = captchaManager.getCaptcha(code, mobile);
        if (captcha == null || !captcha.getMobile().equals(mobile)) {
            throw new GlobalException("验证码无效！");
        }
        if (!captcha.getCaptcha().equals(captchaCode)) {
            throw new GlobalException("验证码错误！");
        }
    }
}
